﻿using System.Collections.Generic;
using System.Linq;
using ML.Entity;
using ML.IRepository;
using Dapper;

namespace ML.Repository
{
    public class UserInvitationRepository : DBFactory, IUserInvitationRepository
    {
        public IEnumerable<UserInvitationResponse> GetUserInvitationByUserId(string id)
        {
            const string sql = @"SELECT u.Id UserId,u.NickName,uv.AddType,uv.ContentSource,uv.DelMark,u.CreateTime FROM ml_user u 
	                INNER JOIN ml_userinvitation uv on u.Id=uv.BeInvitationId
                WHERE uv.InvitationId=@InvitationId";
            using (_db)
            {
                return _db.Query<UserInvitationResponse>(sql, new { @InvitationId = id });
            }
        }

        public bool CheckAlreadyInvited(string invitationId, string beInvitationId)
        {
            const string sql = @"SELECT * from ml_userinvitation WHERE InvitationId=@InvitationId AND BeInvitationId=@BeInvitationId";
            bool isTrue = false;
            using (_db)
            {
                //检查 每个被邀请的用户只能被邀请一次
                var r = _db.Query<UserInvitationResponse>(sql, new { @InvitationId = invitationId, @BeInvitationId = beInvitationId }).Any();
                if (r)
                {
                    isTrue = true;
                }
            }
            return isTrue;
        }

        public bool Add(UserInvitationEntity entity)
        {
            const string sql = @"INSERT INTO ml_userinvitation(Id,InvitationId,BeInvitationId,AddType,ContentSource,DelMark) 
                     VALUES (@Id,@InvitationId,@BeInvitationId,@AddType,@ContentSource,@DelMark);";
            using (_db)
            {
                if (_db.Execute(sql, entity) > 0)
                {
                    return true;
                }
                return false;
            }
        }

        public bool Delete(UserInvitationEntity entity)
        {
            throw new System.NotImplementedException();
        }

        public bool Delete(string id)
        {
            throw new System.NotImplementedException();
        }

        public UserInvitationEntity GetById(string id)
        {
            throw new System.NotImplementedException();
        }

        public bool Update(UserInvitationEntity entity)
        {
            throw new System.NotImplementedException();
        }
    }
}